home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
By the Book
/
Mac C Primer V2
/
3.4 - DLOG
/
DLOG.c
next >
Wrap
C/C++ Source or Header
|
1991-08-20
|
3KB
|
160 lines
/************************************************************/
/* */
/* DLOG Code from Chapter Three of */
/* */
/* *** The Macintosh Programming Primer *** */
/* */
/* Copyright 1990, Dave Mark */
/* */
/* This program demonstrates specific Mac programming */
/* techniques. */
/* */
/************************************************************/
#define BASE_RES_ID 400
#define NIL_POINTER 0L
#define MOVE_TO_FRONT (WindowPtr)-1L
#define REMOVE_ALL_EVENTS 0
#define OK_ITEM 1
#define CANCEL_ITEM 2
#define TEXT_ITEM 4
#define TE_ENTER_KEY 3
#define TE_TAB_CHAR 9
#define TE_CARRIAGE_RETURN 13
pascal Boolean DLOGFilter();
main()
{
DialogPtr theDialog;
Boolean done;
int itemHit, itemType;
Handle OKHandle, textHandle;
Rect itemRect;
Str255 theText;
ToolBoxInit();
theDialog = GetNewDialog( BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT );
GetDItem( theDialog, OK_ITEM, &itemType, &OKHandle, &itemRect );
GetDItem( theDialog, TEXT_ITEM, &itemType, &textHandle, &itemRect );
CenterDialog( theDialog );
ShowWindow( theDialog );
SetPort( theDialog );
DrawOKButton( theDialog );
done = FALSE;
while ( ! done )
{
GetIText( textHandle, &theText );
if ( theText[ 0 ] == 0 )
HiliteControl( OKHandle, 255 );
else
HiliteControl( OKHandle, 0 );
ModalDialog( DLOGFilter, &itemHit );
done = ( (itemHit == OK_ITEM) || (itemHit == CANCEL_ITEM) );
}
}
/*********************************** ToolBoxInit */
ToolBoxInit()
{
InitGraf( &thePort );
InitFonts();
FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
InitWindows();
InitMenus();
TEInit();
InitDialogs( NIL_POINTER );
InitCursor();
}
/*********************************************** DLOGFilter *****/
pascal Boolean DLOGFilter( theDialog, e, iPtr )
DialogPtr theDialog;
EventRecord *e;
int *iPtr;
{
int itemType;
Rect itemRect;
Handle item;
Str255 tempStr;
char theChar;
GetDItem( theDialog, TEXT_ITEM, &itemType, &item, &itemRect );
GetIText( item, &tempStr );
if (e->what == keyDown)
{
theChar = (e->message & charCodeMask);
if ( (theChar == TE_CARRIAGE_RETURN) || (theChar == TE_ENTER_KEY) )
{
if ( tempStr[ 0 ] != 0 )
{
*iPtr = OK_ITEM;
GetDItem( theDialog, OK_ITEM, &itemType, &item, &itemRect );
HiliteControl( item, 1 );
return( TRUE );
}
else
{
*iPtr = TEXT_ITEM;
return( TRUE );
}
}
}
return( FALSE );
}
/*********************************************** DrawOKButton *****/
DrawOKButton( theDialog )
DialogPtr theDialog;
{
int itemType;
Rect itemRect;
Handle item;
GrafPtr oldPort;
GetDItem( theDialog, OK_ITEM, &itemType, &item, &itemRect );
GetPort( &oldPort );
SetPort( theDialog );
PenSize( 3, 3 );
InsetRect( &itemRect, -4, -4 );
FrameRoundRect( &itemRect, 16, 16 );
PenNormal();
SetPort( oldPort );
}
/*********************************************** CenterDialog *****/
CenterDialog( theDialog )
DialogPtr theDialog;
{
Rect r;
int width, height, sWidth, sHeight, h, v;
r = theDialog->portRect;
width = r.right - r.left;
height = r.bottom - r.top;
sWidth = screenBits.bounds.right - screenBits.bounds.left;
sHeight = screenBits.bounds.bottom - screenBits.bounds.top;
h = (sWidth - width) / 2;
v = (sHeight - height) / 2;
MoveWindow( theDialog, h, v, FALSE );
}